package cn.xyinping.shoppingcart.controller;

import cn.xyinping.basic.exception.MyException;
import cn.xyinping.basic.util.AjaxResult;
import cn.xyinping.basic.util.LoginContextUtil;
import cn.xyinping.basic.util.PageList;
import cn.xyinping.shoppingcart.domain.ShoppingCart;
import cn.xyinping.shoppingcart.query.ShoppingCartQuery;
import cn.xyinping.shoppingcart.service.IShoppingCartService;
import cn.xyinping.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 27266
 * @desrciption 购物车
 * @date 2021/11/10
 */
@RestController
@RequestMapping("/cart")
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shoppingCartService;
    // 添加购物车
    @PutMapping("addcarts")
    public AjaxResult addCart(@RequestBody Long id) {
        try {
            ShoppingCart shoppingCart = shoppingCartService.addCart(id);
            return AjaxResult.me().setResultObj(shoppingCart);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }
    // 查询购物车
    @PostMapping("/carts")
    public AjaxResult carts(){
        try {
            List<ShoppingCart> list = shoppingCartService.loadCarte();
            return AjaxResult.me().setResultObj(list);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }


}
